<?php

class RatsFormatTest extends UnitTestCase {
    function TestRatsClientlib() {
        $this->UnitTestCase("Rats Format Tests");
    }

    // # we don't use the regular setUp and tearDown functions here, cause this would cause too much traffic
    // # instead we're relying on the fact that methods are executed in declaration order
    // function testSetup() {
    //   try {
    //     $rats = new Rats(SUPERUSER, SUPERPWD, BASE);
    //     $newUser = $rats->createUser(USER, EMAIL, PWD);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     assert(False);
    //   }
    // }
    
    function testCreateBasicMp4Format() {
      try {
        # create format profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        $rats->setSSL(SSL);
        $video_params = array("video_codec" => "mpeg4");
        $audio_params = array("audio_codec" => "aac");
        $format_entry = $rats->createFormat(FORMAT_MP4, DESCRIPTION, CONTAINER_MP4, CATEGORY_FORMATS, $video_params, $audio_params);
        $this->assertEqual($format_entry->content->params->name->text, FORMAT_MP4);
        # retrieve the format profile
        $format2_entry = $rats->getFormatEntry($format_entry->id->text);
        $this->assertEqual($format_entry->content->params->name->text, $format2_entry->content->params->name->text);
        # retrieve the format profile using the id as input
        $format3_entry = $rats->getFormatEntryFromId($format_entry->content->params->id->text);
        $this->assertEqual($format2_entry->content->params->name->text, $format3_entry->content->params->name->text);
        # delete the format
        $rats->deleteFormat($format_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

    function testCheckAudioVideoParams() {
      try {
        # video params
        $video_codec = "h264";
        $video_bitrate = "360";
        $video_fps = "23.98";
        $video_width = "512";
        $video_height = "256";
        $video_deinterlace = 'no';
        $video_cq = '0.11';
        $video_passes = '2';
        $video_crop_top = '10';
        $video_crop_bottom = '2';
        $video_crop_right = '4';
        $video_crop_left = '4';
        $video_params = array("video_codec" => $video_codec,
                              "video_bitrate" => $video_bitrate,
                              "video_fps" => $video_fps,
                              "video_width" => $video_width,
                              "video_height" => $video_height,
                              "video_deinterlace" => $video_deinterlace,
                              "video_cq" => $video_cq,
                              "video_passes" => $video_passes,
                              "video_crop_top" => $video_crop_top,
                              "video_crop_bottom" => $video_crop_bottom,
                              "video_crop_right" => $video_crop_right,
                              "video_crop_left" => $video_crop_left);
        # audio params
        $audio_channel = "1";
        $audio_codec = "aac";
        $audio_bitrate = "72";
        $audio_sample_rate = "44100";
        $audio_passes = "2";
        $audio_track_mapping = "0.1:0.1";
        $audio_params = array("audio_channel" => $audio_channel,
                              "audio_codec" => $audio_codec,
                              "audio_bitrate" => $audio_bitrate,
                              "audio_sample_rate" => $audio_sample_rate,
                              "audio_passes" => $audio_passes,
                              "audio_track_mapping" => $audio_track_mapping);
        # hinting
        $hinting = "1";
        # create format profile
        $rats = new Rats(USER, PWD, BASE);
        $rats->setSSL(SSL);
        $format_entry = $rats->createFormat(FORMAT_MP4, DESCRIPTION, CONTAINER_MP4, CATEGORY_FORMATS, $video_params, $audio_params, $hinting);
        
        $this->assertEqual($rats->getResponse()->getStatus(), 201);
        $this->assertEqual($format_entry->content->params->name->text, FORMAT_MP4);
        $this->assertEqual($format_entry->content->params->description->text, DESCRIPTION);
        $this->assertEqual($format_entry->content->params->container->text, CONTAINER_MP4);
        $this->assertEqual($format_entry->content->params->category->text, CATEGORY_FORMATS);
        
        $this->assertEqual($format_entry->content->params->video_codec->text, $video_codec);
        $this->assertEqual($format_entry->content->params->video_bitrate->text, $video_bitrate);
        $this->assertEqual($format_entry->content->params->video_fps->text, $video_fps);
        $this->assertEqual($format_entry->content->params->video_width->text, $video_width);
        $this->assertEqual($format_entry->content->params->video_height->text, $video_height);
        $this->assertEqual($format_entry->content->params->video_deinterlace->text, $video_deinterlace);
        $this->assertEqual($format_entry->content->params->video_cq->text, $video_cq);
        $this->assertEqual($format_entry->content->params->video_passes->text, $video_passes);
        $this->assertEqual($format_entry->content->params->video_crop_top->text, $video_crop_top);
        $this->assertEqual($format_entry->content->params->video_crop_bottom->text, $video_crop_bottom);
        $this->assertEqual($format_entry->content->params->video_crop_right->text, $video_crop_right);
        $this->assertEqual($format_entry->content->params->video_crop_left->text, $video_crop_left);

        $this->assertEqual($format_entry->content->params->audio_channel->text, $audio_channel);
        $this->assertEqual($format_entry->content->params->audio_codec->text, $audio_codec);
        $this->assertEqual($format_entry->content->params->audio_bitrate->text, $audio_bitrate);
        $this->assertEqual($format_entry->content->params->audio_sample_rate->text, $audio_sample_rate);
        $this->assertEqual($format_entry->content->params->audio_passes->text, $audio_passes);
        $this->assertEqual($format_entry->content->params->audio_track_mapping->text, $audio_track_mapping);

        $this->assertEqual($format_entry->content->params->hinting->text, $hinting);
               
        # retrieve the format profile using the id as input
        $format2_entry = $rats->getFormatEntryFromId($format_entry->content->params->id->text);

        $this->assertEqual($format2_entry->content->params->name->text, FORMAT_MP4);
        $this->assertEqual($format2_entry->content->params->description->text, DESCRIPTION);
        $this->assertEqual($format2_entry->content->params->container->text, CONTAINER_MP4);
        $this->assertEqual($format2_entry->content->params->category->text, CATEGORY_FORMATS);
        
        $this->assertEqual($format2_entry->content->params->video_codec->text, $video_codec);
        $this->assertEqual($format2_entry->content->params->video_bitrate->text, $video_bitrate);
        $this->assertEqual($format2_entry->content->params->video_fps->text, $video_fps);
        $this->assertEqual($format2_entry->content->params->video_width->text, $video_width);
        $this->assertEqual($format2_entry->content->params->video_height->text, $video_height);
        $this->assertEqual($format2_entry->content->params->video_deinterlace->text, $video_deinterlace);
        $this->assertEqual($format2_entry->content->params->video_cq->text, $video_cq);
        $this->assertEqual($format2_entry->content->params->video_passes->text, $video_passes);
        $this->assertEqual($format2_entry->content->params->video_crop_top->text, $video_crop_top);
        $this->assertEqual($format2_entry->content->params->video_crop_bottom->text, $video_crop_bottom);
        $this->assertEqual($format2_entry->content->params->video_crop_right->text, $video_crop_right);
        $this->assertEqual($format2_entry->content->params->video_crop_left->text, $video_crop_left);

        $this->assertEqual($format2_entry->content->params->audio_channel->text, $audio_channel);
        $this->assertEqual($format2_entry->content->params->audio_codec->text, $audio_codec);
        $this->assertEqual($format2_entry->content->params->audio_bitrate->text, $audio_bitrate);
        $this->assertEqual($format2_entry->content->params->audio_sample_rate->text, $audio_sample_rate);
        $this->assertEqual($format2_entry->content->params->audio_passes->text, $audio_passes);
        $this->assertEqual($format2_entry->content->params->audio_track_mapping->text, $audio_track_mapping);
        
        # delete the format
        $rats->deleteFormat($format_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $this->assertEqual("success", $e->getResponse()->getBody());
      }
    }
  
    
    
    // function testCreateFtpFormat() {
    //   try {
    //     # create format profile (rass, no base uri)
    //     $rats = new Rats(USER, PWD, BASE);
    //     $format_entry = $rats->createFormat(FORMAT_FTP, "I'm from Barcelona", METHOD_FTP, null, "ftp://myfileserver.glugle.org/fawlty/", "http://publicserver.glugle.com/wadde/");
    //     $this->assertEqual($rats->getResponse()->getStatus(), 201);
    //     $this->assertEqual($format_entry->content->params->name->text, FORMAT_FTP);
    //     # delete the format
    //     $rats->deleteFormat($format_entry->id->text);
    //     $this->assertEqual($rats->getResponse()->getStatus(), 204);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     assert(False);
    //   }
    // }
    // 
    // function testCreateFtpFormatNoBaseUri() {
    //   try {
    //     # create format profile (rass, no base uri)
    //     $rats = new Rats(USER, PWD, BASE);
    //     $format_entry = $rats->createFormat(FORMAT_FTP, null, METHOD_FTP, null);
    //     $this->assertEqual($rats->getResponse()->getStatus(), 201);
    //     $this->assertEqual($format_entry->content->params->name->text, FORMAT_FTP);
    //     # delete the format
    //     $rats->deleteFormat($format_entry->id->text);
    //     $this->assertEqual($rats->getResponse()->getStatus(), 204);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     assert(False);
    //   }
    // }
    // 
    // function testCheckCreatedParams() {
    //   try {
    //     # create format profile (rass, no base uri)
    //     $rats = new Rats(USER, PWD, BASE);
    //     $allow_overwrite = "1";
    //     $max_size = "300";
    //     $params = "<some>monster</some>";
    //     $format_entry = $rats->createFormat(FORMAT_FTP, DESCRIPTION, METHOD_FTP, null, BASE_URI, PUBLIC_URI, $allow_overwrite, $max_size, $params);
    //     $this->assertEqual($rats->getResponse()->getStatus(), 201);
    //     $this->assertEqual($format_entry->content->params->name->text, FORMAT_FTP);
    //     $this->assertEqual($format_entry->content->params->description->text, DESCRIPTION);
    //     $this->assertEqual($format_entry->content->params->method->text, METHOD_FTP);
    //     $this->assertEqual($format_entry->content->params->public_uri->text, PUBLIC_URI);
    //     $this->assertEqual($format_entry->content->params->base_uri->text, BASE_URI);
    //     $this->assertEqual($format_entry->content->params->allow_overwrite->text, $allow_overwrite);
    //     $this->assertEqual($format_entry->content->params->max_size->text, $max_size);
    //     $this->assertEqual($format_entry->content->params->params->text, $params);
    //     # retrieve the format profile using the id as input
    //     $format2_entry = $rats->getFormatEntryFromId($format_entry->content->params->id->text);
    //     $this->assertEqual($format2_entry->content->params->name->text, FORMAT_FTP);
    //     $this->assertEqual($format2_entry->content->params->description->text, DESCRIPTION);
    //     $this->assertEqual($format2_entry->content->params->method->text, METHOD_FTP);
    //     $this->assertEqual($format2_entry->content->params->public_uri->text, PUBLIC_URI);
    //     $this->assertEqual($format2_entry->content->params->base_uri->text, BASE_URI);
    //     $this->assertEqual($format2_entry->content->params->allow_overwrite->text, $allow_overwrite);
    //     $this->assertEqual($format2_entry->content->params->max_size->text, $max_size);
    //     $this->assertEqual($format2_entry->content->params->params->text, $params);
    //     
    //     # delete the format
    //     $rats->deleteFormat($format_entry->id->text);
    //     $this->assertEqual($rats->getResponse()->getStatus(), 204);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     assert(False);
    //   }
    // }
    // 
    // function testCreateFormatWithSameName() {
    //   $status = 0;
    //   try {
    //     # create format profile (rass, no base uri)
    //     $rats = new Rats(USER, PWD, BASE);
    //     $format_entry = $rats->createFormat(FORMAT_RASS, "Me know nothing", METHOD_RASS, CDN01);
    //     try {
    //       $format_entry = $rats->createFormat(FORMAT_RASS, null, METHOD_FTP, null);
    //       assert(False);
    //     }
    //     catch(Zend_Gdata_App_Exception $e) {
    //       $status = $e->getResponse()->getStatus();
    //     }
    //     $rats->deleteFormat($format_entry->id->text);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     $status = $e->getResponse()->getStatus();
    //   }
    //   $this->assertEqual($status, 400); 
    // }
    // 
    // function testCreateInvalidFormatNoMethod() {
    //   $status = 0;
    //   try {
    //     # create format profile (rass, no base uri)
    //     $rats = new Rats(USER, PWD, BASE);
    //     # no method, so should not be accepted
    //     $format_entry = $rats->createFormat(FORMAT_FTP, "Me know nothing", null, CDN01, "ftp://myfileserver.glugle.org/fawlty/", "http://publicserver.glugle.com/wadde/");
    //     assert(False);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     $status = $e->getResponse()->getStatus();
    //   }
    //   $this->assertEqual($status, 400); 
    // }
    // 
    // function testCreateInvalidMethod() {
    //   $status = 0;
    //   try {
    //     # create format profile (rass, no base uri)
    //     $rats = new Rats(USER, PWD, BASE);
    //     # no method, so should not be accepted
    //     $format_entry = $rats->createFormat(FORMAT_FTP, "Me know nothing", "gaga", null, "ftp://myfileserver.glugle.org/fawlty/", "http://publicserver.glugle.com/wadde/");
    //     assert(False);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     $status = $e->getResponse()->getStatus();
    //   }
    //   $this->assertEqual($status, 400); 
    // }
    // 
    // 
    // function testCreateInvalidFormatNoName() {
    //   $status = 0;
    //   try {
    //     # create format profile (rass, no base uri)
    //     $rats = new Rats(USER, PWD, BASE);
    //     # no method, so should not be accepted
    //     $format_entry = $rats->createFormat(null, "Me know nothing", METHOD_RASS, CDN01, "ftp://myfileserver.glugle.org/fawlty/", "http://publicserver.glugle.com/wadde/");
    //     assert(False);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     $status = $e->getResponse()->getStatus();
    //   }
    //   $this->assertEqual($status, 400); 
    // }
    // 
    // function testCreateInvalidFormatRassNoCDN() {
    //   $status = 0;
    //   try {
    //     # create format profile (rass, no base uri)
    //     $rats = new Rats(USER, PWD, BASE);
    //     # no method, so should not be accepted
    //     $format_entry = $rats->createFormat(FORMAT_RASS, "Me know nothing", METHOD_RASS, null, "ftp://myfileserver.glugle.org/fawlty/", "http://publicserver.glugle.com/wadde/");
    //     assert(False);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     $status = $e->getResponse()->getStatus();
    //   }
    //   $this->assertEqual($status, 400); 
    // }
    // 
    // function testCreateInvalidFormatFtpWrongBaseUri() {
    //   $status = 0;
    //   try {
    //     # create format profile (rass, no base uri)
    //     $rats = new Rats(USER, PWD, BASE);
    //     # no method, so should not be accepted
    //     $format_entry = $rats->createFormat(FORMAT_FTP, "Me know nothing", METHOD_FTP, null, "hi/this_is_nice");
    //     assert(False);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     $status = $e->getResponse()->getStatus();
    //   }
    //   $this->assertEqual($status, 400); 
    // }
    // 
    // function testCreateInvalidFormatRassWrongBaseUri() {
    //   $status = 0;
    //   try {
    //     # create format profile (rass, no base uri)
    //     $rats = new Rats(USER, PWD, BASE);
    //     # no method, so should not be accepted
    //     $format_entry = $rats->createFormat(FORMAT_RASS, "Me know nothing", METHOD_RASS, CDN01, "http://doom.com/");
    //     assert(False);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     $status = $e->getResponse()->getStatus();
    //   }
    //   $this->assertEqual($status, 400); 
    // }


    // 
    // function testTeardown() {
    //   try {
    //     $rats = new Rats(SUPERUSER, SUPERPWD, BASE);
    //     $newUser = $rats->deleteUser(USER);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     assert(False);
    //   }
    // }
}
